#include <sstream>
#include "matchordermgr.h"

MatchOrderData::MatchOrderData():
    uid(0),
    game(0),
    matchtype(0),
    agencyid(0),
    fee(0),
    settlement(0)
{

}

MatchOrderData& MatchOrderData::operator = (const MatchOrderData& that)
{
    this->uid = that.uid;
    this->game = that.game;                    
    this->matchtype = that.matchtype;                 
    this->agencyid = that.agencyid;                 
    this->currency = that.currency;                  
    this->regid = that.regid;                      
    this->unregid = that.unregid;                   
    this->settlementid = that.settlementid;                  
    this->fee = that.fee;                       
    this->settlement = that.settlement;  
    
    return *this;
}

MatchOrderData& MatchOrderData::operator = (MatchOrderData&& that)
{
    this->uid = that.uid;
    this->game = that.game;                    
    this->matchtype = that.matchtype;                 
    this->agencyid = that.agencyid;                 
    this->currency = std::move(that.currency);                  
    this->regid = std::move(that.regid);                      
    this->unregid = std::move(that.unregid);                   
    this->settlementid = std::move(that.settlementid);                  
    this->fee = that.fee;                       
    this->settlement = that.settlement;  
    
    return *this;
}


std::string MatchOrderData::ToString() const 
{
    std::stringstream ss;
    ss << "{" 
        << "uid:" << this->uid << ","
        << "game:" << this->game << ","
        << "matchtype:" << this->matchtype << ","
        << "agencyid:" << this->agencyid << ","
        << "currency:" << this->currency << ","
        << "regid:" << this->regid << ","
        << "unregid:" << this->unregid << ","
        << "settlementid:" << this->settlementid << ","
        << "fee:" << this->fee << ","
        << "settlement:" << this->settlement
    << "}";

    return ss.str();
}


MatchOrderMgr::MatchOrderMgr()
{
}
MatchOrderMgr::~MatchOrderMgr()
{
}

std::int32_t MatchOrderMgr::Get(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, MatchOrderData& order_data)
{
    MATCHORDERMGRMAP::iterator iter = m_MatchOrders.find(matchid);
    if (iter == m_MatchOrders.end())
    {
        return 0;
    }

    MATCHORDERMAP& orderMap = m_MatchOrders[matchid];
    MATCHORDERMAP::iterator it = orderMap.find(uid);
    if (it == orderMap.end())
        return 0;

    order_data = it->second;
    return 1;
}

void MatchOrderMgr::CreateMatch(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, MatchOrderData&& order_data)
{
    m_MatchOrders[matchid][uid] = std::move(order_data);
}

void MatchOrderMgr::MatchReg(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, const std::string& regid, std::int64_t fee)
{
    MatchOrderData& order_data = m_MatchOrders[matchid][uid];
    order_data.regid = regid;
    order_data.fee = fee;
    order_data.unregid = "";
}

void MatchOrderMgr::MatchUnReg(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, const std::string& unregid)
{
    MatchOrderData& order_data = m_MatchOrders[matchid][uid];
    order_data.unregid = unregid;
}

void MatchOrderMgr::MatchSettlement(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, const std::string& settlementid, std::int64_t settlement)
{
    MatchOrderData& order_data = m_MatchOrders[matchid][uid];
    order_data.settlement = settlement;
    order_data.settlementid = settlementid;
}


EKBEvent_Error MatchOrderMgr::CheckMatchReg(std::uint32_t uid, const std::string& gamePlat, std::uint64_t matchid, const std::string& order, std::int64_t fee, std::int32_t matchtime, std::int32_t &firstReg)
{
    // 加载数据
    firstReg = 0;
    MatchOrderData order_data;
    std::int32_t ret = Get(uid, matchid, matchtime, gamePlat, order_data);
    if (0 > ret) return KB_ERR_INNER_DB_ERROR;  // 请求错误

    if (ret > 0) // 已存在报名场景,不处理循环报名场景
    {
        if (!order_data.settlementid.empty())    //已报名，已结算
        {
            return KB_ERR_ALREADY_MATCHSETTLE;
        }
        if (!order_data.unregid.empty())         //已报名，已取消
        {
            return KB_ERR_NONE;
        }
        else
        {
            return KB_ERR_ALREADY_MATCHREG;      //已报名
        }
    }
    else
    {
        firstReg = 1;
        return KB_ERR_NONE;
    }
}
EKBEvent_Error MatchOrderMgr::CheckMatchUnReg(std::uint32_t uid, const std::string& gamePlat, std::uint64_t matchid, const std::string& order, std::int32_t matchtime, std::int64_t& fee)
{
    // 加载数据
    MatchOrderData order_data;
    std::int32_t ret = Get(uid, matchid, matchtime, gamePlat, order_data);
    if (0 > ret) return KB_ERR_INNER_DB_ERROR;  // 请求错误
    if (ret == 0) return KB_ERR_NOT_MATCHREG; // 查询条数为0
    if (0 != order_data.regid.compare(order)) return KB_ERR_NOT_MATCHORDER;  //订单不一致
    if (!order_data.unregid.empty()) return KB_ERR_ALREADY_MATCHUNREG;  //已取消
    if (!order_data.settlementid.empty()) return KB_ERR_ALREADY_MATCHSETTLE;  //已结算

    fee = order_data.fee;
    return KB_ERR_NONE;
}

EKBEvent_Error MatchOrderMgr::CheckMatchSettlement(std::uint32_t uid, const std::string& gamePlat, std::uint64_t matchid, const std::string& order, std::int64_t settlement, std::int32_t matchtime)
{
    MatchOrderData order_data;
    std::int32_t ret = MatchOrderMgr::Instance()->Get(uid, matchid, matchtime, gamePlat, order_data);
    if (0 > ret) return KB_ERR_INNER_DB_ERROR;  // 请求错误
    if (ret == 0) return KB_ERR_NOT_MATCHREG; // 查询条数为0
    if (order_data.regid.empty()) return KB_ERR_NOT_MATCHREG;  //未报名
    if (!order_data.unregid.empty()) return KB_ERR_ALREADY_MATCHUNREG;  //已取消
    if (!order_data.settlementid.empty()) return KB_ERR_ALREADY_MATCHSETTLE;  //已结算
    return KB_ERR_NONE;
}
